package cn.jhc.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Title;
import cn.jhc.dao.DaoType;
import cn.jhc.dao.TitleDAO;
import cn.jhc.factory.DAOFactory;

/**
 * Servlet implementation class ListServlet
 */
@WebServlet("/list.htm")
public class ListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final static Logger logger = Logger.getLogger(ListServlet.class.getName()); 

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("page");
		int page = 1;
		if (param == null)
			page = 1;
		else
			page = Integer.parseInt(param);
		TitleDAO dao = (TitleDAO)DAOFactory.getDao(DaoType.TITLE);
		int total = dao.getRowsCount() / 8 + 1;
		List<Title> titles = dao.getTitlesInPage(page * 8 - 8, 8);
		logger.info("图书一共有" + total + "页。");

		// int total = dao.getRowsCount() / 8;

		request.setAttribute("titles", titles);
		request.setAttribute("currentPage", page);
		request.setAttribute("total", total);
		request.getRequestDispatcher("/list.jsp").forward(request,
				response);
	}

}
